package com.qf.demo;

import com.qf.mapper.AccountRowMapper;
import com.qf.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.List;


/**
 * @Description: 公用常量
 * @Data: 2022/9/1 10:29
 * @Pacakge: com.qf.demo
 * @ClassName: Demo
 * @Author: fan
 */
public class Demo {
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/spring");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("f971014");
        jdbcTemplate.setDataSource(driverManagerDataSource);
        //execute:直接执行sql语句
        jdbcTemplate.execute("select * from account where id = 8");
        //update表示可以执行增删改操作
        //int update = jdbcTemplate.update("update account set name = ?,money=? where id = ?", "小樊", 100.00, 3);
        //System.out.println("影响了" + update+"行");
       //int insert = jdbcTemplate.update("insert into account (name,money) values (?,?)", "小尹", 1000.00);
        //System.out.println("影响了" + insert+"行");
        //int delete = jdbcTemplate.update("delete from account where id = ?", 11);
        //System.out.println("影响了"+delete+"行");
        List<Account> list = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        for (Account account:list){
            System.out.println("account = " + account);
        }
        List<Account> query = jdbcTemplate.query("select * from account where id > ? and money > ?", new AccountRowMapper(), 2, 500.00);
        for (Account account01:query){
            System.out.println("account01 = " + account01);
        }
    }
}
